<?php
namespace Admin\Controller;
use Think\Controller;
class CareCenterController  extends IndexController {
    public function lists(){
        $type = I('request.type');
        if($type==1){
            $CareCenter=D("Carecenter");
            $data['code']=I("post.code");
            $data['name']=I("post.name");
            $data['addr']=I("post.addr");
            $data['telephone']=I("post.telephone");
            $data['introduce']=I("post.introduce");
            $data['price']=I('post.price');
            $data['is_hz']=I("post.is_hz");
            if(empty($data['is_hz'])){
                $data["is_hz"]=0;
            }
            if (is_array($_FILES['imgs'][0]['name'])||$_FILES['imgs']!=null) {
                $file = $_FILES;
                $newFile = array();
                foreach ($file as $key => $value) {
                    $num = count($value['name']);
                    for ($i = 0; $i < $num; $i++) {
                        $newFile[$i]['name'] = $value['name'][$i];
                        $newFile[$i]['type'] = $value['type'][$i];
                        $newFile[$i]['tmp_name'] = $value['tmp_name'][$i];
                        $newFile[$i]['error'] = $value['error'][$i];
                        $newFile[$i]['size'] = $value['size'][$i];
                    }
                }
                header("content-type:text/html;charset=utf-8");
                $upload = new \Think\Upload(); // 实例化上传类
                $upload->maxSize = 1024 * 1024 * 10;// 设置附件上传大小 (-1) 是不限值大小
                $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = "./imageCareCenter_xx/";
                $upload->replace = true; //存在同名文件是否是覆盖
                // 是否使用子目录保存上传文件
                $upload->autoSub = true;
                $info = $upload->upload($newFile);
                if (!$info) {
                    $data['imgs'] = "";
                } else {
                    //拼接上传的图片路径用于上传到服务器
                    $path = '';
                    for ($i = 0; $i <= count($info) - 1; $i++) {
                        $path .= ltrim($info[$i]['savepath'], './') . $info[$i]['savename'] . ',';
                    }
                       $data['imgs']=$path;
                }
            }
            if($_FILES['pics']!=null){
                $pics=$_FILES['pics'];
                header("content-type:text/html;charset=utf-8");
                $upload = new \Think\Upload(); // 实例化上传类
                $upload->maxSize = 1024 * 1024 * 10;// 设置附件上传大小 (-1) 是不限值大小
                $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = "./imageCareCenter/";
                $upload->replace = true; //存在同名文件是否是覆盖
                // 是否使用子目录保存上传文件
                $upload->autoSub = true;
                $info = $upload->uploadOne($pics);
                if (!$info) {
                    $path= "";
                } else {
                        $path= ltrim($info['savepath'], './') . $info['savename'] ;
                        $data['pics']=$path;
                  }
            }
            $CareCenter->add($data);
            $this->redirect("CareCenter/lists");
        }
        if ($type == 2) {//处理修改的内容，调用actionUpdate方法

           $this->actionUpdate();

          $this->redirect('CareCenter/lists');
        }
        $this->display();

    }


//=======================获得月子中心列表===================================
    public function get_careCenter_list(){
        $careCenter= D("Carecenter");

        if(I("request.iDisplayStart")==''){

            $start=0;
        }else{
            $start=I("request.iDisplayStart");
        }
        if(I("request.sSearch")!=''){
            $where['name'] = array('like','%'.I("request.sSearch").'%');;
        }else{
            $where='';
        }
        $result=$careCenter->field("id,code,name,addr,telephone,introduce,pics,imgs,is_hz")
            ->limit($start,I("request.iDisplayLength"))
            ->select();
        foreach ( $result as $key=>&$value) {
            $value['is_hz'] = $value['is_hz']==0 ? "未合作" : "已合作";
            }
        if(!$result) {
            echo json_encode(0);exit;
        }

        $ret = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $start,
            "iTotalDisplayRecords" => $careCenter->count(),
            "aaData" => $result
        );

        echo json_encode($ret);exit;
    }
    //====================增加月子中心的display()模板==============================
    public function add(){
        $this->display();
    }
    //====================修改月子中心==========================
    public function update(){
        $id=I("request.id");
        setcookie("uid", $id, time()+3600);
        $this->display();
    }
    //获取修改列表
    public function update_list(){
        $id=$_COOKIE["uid"];
        $careCenter=D("Carecenter");
        $r=$careCenter->where('id='.$id)->field('code,name,telephone,addr,introduce,pics,imgs,is_hz')->find();
        if(!$r){
            echo json_encode(0);exit;
        }
             echo json_encode($r);exit;
    }
    //==============================获得要修改形象图================================================
    public function get_imgs(){
        $id = $_COOKIE['uid'];
        $careCenter = D('Carecenter');
        $imgsWhere="id=".$id;
        $imgs=$careCenter->where($imgsWhere)->getField("imgs");
        $str=rtrim($imgs,',');
        $result=explode(',',$str);

       if (!$result) {
             echo json_encode(0); exit;
           }
             echo json_encode($result); exit;

         }

    //=============================用于删除从数据库修改页面获得的图片 ===============================
    public function update_imgs(){
        $id=$_COOKIE["uid"];
        $imgNum = I("request.imgNum");
        $careCenter=D("Carecenter");
        $where="id=".$id;
        $imgs = $careCenter->where($where)->getField('imgs');


        $str = str_replace($imgNum . ',', "", $imgs);
        $data['imgs'] = $str;
        unlink("./imageCareCenter_xx/" . $imgNum);
        $where="id=".$id;
        $careCenterResult = $careCenter->where($where)->save($data);
        if (!$careCenterResult) {
            echo json_encode(0); exit;
        } else {
            echo json_encode($careCenterResult); exit;
        }
    }
  //=========================================处理修改月子中心修改的内容======================================================

    public function actionUpdate(){
        $CareCenter = D('Carecenter');
        $data['name'] = I("post.name");
        $data['hid'] = I("post.hid");
        $data['addr'] = I("post.addr");
        $data['telephone'] = I("post.telephone");
        $data['introduce'] = I("post.introduce");
        $data['price'] = I("post.price");
        //{是否是合作商家使用的是checkbox如果单选按钮，如果按钮没有选中的话，提交的表单里会没有is_hd这个字段=====================================
        if($_POST['is_hz']==null){
            $data['is_hz']=0;
        }else{
            $data['is_hz']=I("post.is_hz");
        }
    //=================================================================}
        if ($_FILES['imgs']['name'][0] != null) {
            $file = $_FILES;
            $newFile = array();
            foreach ($file as $key => $value) {
                $num = count($value['name']);
                for ($i = 0; $i < $num; $i++) {
                    $newFile[$i]['name'] = $value['name'][$i];
                    $newFile[$i]['type'] = $value['type'][$i];
                    $newFile[$i]['tmp_name'] = $value['tmp_name'][$i];
                    $newFile[$i]['error'] = $value['error'][$i];
                    $newFile[$i]['size'] = $value['size'][$i];
                }
            }
            header("content-type:text/html;charset=utf-8");
            $upload = new \Think\Upload(); // 实例化上传类
            $upload->maxSize = 1024 * 1024 * 10;// 设置附件上传大小 (-1) 是不限值大小
            $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = "./imageCareCenter_xx/";
           //$upload->savePath = "./Hotel";// 设置附件上传目录
            $upload->replace = true; //存在同名文件是否是覆盖
            // 是否使用子目录保存上传文件
            $upload->autoSub = true;
            $info = $upload->upload($newFile);
            if (!$info) {
                $data['imgs'] = "";
            } else {
                //拼接上传的图片路径用于上传到服务器
                $path = '';
                for ($i = 0; $i <= count($info) - 1; $i++) {
                    $path .= ltrim($info[$i]['savepath'], './') . $info[$i]['savename'] . ',';
                }
                if (isset($_COOKIE["uid"])) {
                    $getData = $CareCenter->where("id=" . $_COOKIE["uid"])->getField('imgs');
                }

                if ($path != null) {
                    $data['imgs'] = $path . $getData;
                } else {
                    $data['imgs'] = $getData;
                }
            }
        }

        if ($_FILES['pics']['name'] != null) {
            $pics = $_FILES['pics'];
            header("content-type:text/html;charset=utf-8");
            $upload = new \Think\Upload(); // 实例化上传类
            $upload->maxSize = 1024 * 1024 * 10;// 设置附件上传大小 (-1) 是不限值大小
            $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = "./imageCareCenter/";
            $upload->replace = true; //存在同名文件是否是覆盖
            // 是否使用子目录保存上传文件
            $upload->autoSub = true;
            $info = $upload->uploadOne($pics);
            if (!$info) {
                $data['$pics'] = "";
            } else {
                //拼接上传的图片路径用于上传到服务器
               $path = ltrim($info['savepath'], './') . $info['savename'] ;

                if ($path != null) {
                    $data['pics'] = $path;
                }
            }
        }
        if(isset($_COOKIE["uid"])){
            $CareCenter->where("id=".$_COOKIE["uid"])->save($data);
        }


    }
    //=====================================删除数据====================================
    public function del(){
        $id=I("request.id");
        $CareCenter=D("Carecenter");
        $url=$CareCenter->where('id='.$id)->field('pics,imgs')->find();
        $pics=$url['pics'];
        $result=$CareCenter->delete($id);
        unlink("./imageCareCenter/.$pics");
        if(!$result){
            echo json_encode(0);exit;
        }
        echo json_encode($result);exit;
    }


}